package com.airsystem.pos.bengkel.model;

import java.awt.GridBagLayout;
import java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import com.airsystem.pos.bengkel.component.DesktopFrame;
import com.airsystem.pos.bengkel.entity.DetailKendaraan;
import com.airsystem.pos.bengkel.entity.DetailSparepart;
import com.airsystem.pos.bengkel.entity.JenisSparepart;
import com.airsystem.pos.bengkel.entity.Kendaraan;
import com.airsystem.pos.bengkel.entity.Sparepart;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterSparepartModel extends DesktopFrame {
	protected final GridBagLayout baseLayout = new GridBagLayout();
	protected final GridBagLayout panelLayout = new GridBagLayout();

	protected final JPanel basePanel = new JPanel();
	protected final JPanel buttonPanel = new JPanel();

	protected final JLabel merksparepartLabel = new JLabel();
	protected final JLabel submerksparepartLabel = new JLabel();
	protected final JLabel jenisLabel = new JLabel();
	protected final JLabel merkkendaraanLabel = new JLabel();
	protected final JLabel submerkkendaraanLabel = new JLabel();
	protected final JLabel stokLabel = new JLabel();
	protected final JLabel hargaLabel = new JLabel();

	protected final JTextField merksparepartField = new JTextField();
	protected final JTextField submerksparepartField = new JTextField();
	protected final JTextField jenisField = new JTextField();
	protected final JTextField merkkendaraanField = new JTextField();
	protected final JTextField submerkkendaraanField = new JTextField();
	protected final JTextField stokField = new JTextField();
	protected final JTextField hargaField = new JTextField();
	protected final JTextField idField = new JTextField();

	protected final JButton searchButton = new JButton();
	protected final JButton sparepartButton = new JButton();
	protected final JButton vehicleButton = new JButton();
	protected final JButton saveButton = new JButton();
	protected final JButton updateButton = new JButton();
	protected final JButton deleteButton = new JButton();
	protected final JButton cancelButton = new JButton();

	protected Choice choice;

	protected JenisSparepart jenisSparepart;

	public MasterSparepartModel() {
		super();
	}

	protected JTextComponent[] getComponent() {
		return new JTextComponent[] { merksparepartField, submerksparepartField, merkkendaraanField,
									  submerkkendaraanField, jenisField, hargaField,
									  stokField, idField };
	}

	protected DetailSparepart getComponentData() {
		return getDetailSparepart();
	}

	private Sparepart getSparepart() {
		Sparepart sparepart = new Sparepart();
		sparepart.setMerk(merksparepartField.getText().trim().toUpperCase());

		return sparepart;
	}

	private DetailSparepart getDetailSparepart() {
		DetailSparepart detailSparepart = new DetailSparepart();
		detailSparepart.setSub_merk(submerksparepartField.getText().trim().toUpperCase());
		detailSparepart.setSparepart(getSparepart());
		detailSparepart.setHarga_satuan(new BigDecimal(hargaField.getText()));
		detailSparepart.setStok(Long.parseLong(stokField.getText()));
		detailSparepart.setDetail_kendaraan(getDetailKendaraan());
		detailSparepart.setJenis_sparepart(getJenisSparepart());

		return detailSparepart;
	}

	private JenisSparepart getJenisSparepart() {
		JenisSparepart jenisSparepart = new JenisSparepart();
		jenisSparepart.setId(Long.parseLong(idField.getText().trim()));

		return jenisSparepart;
	}

	private Kendaraan getKendaraan() {
		Kendaraan kendaraan = new Kendaraan();
		kendaraan.setMerk(merkkendaraanField.getText().trim());

		return kendaraan;
	}

	private DetailKendaraan getDetailKendaraan() {
		DetailKendaraan detailKendaraan = new DetailKendaraan();
		detailKendaraan.setSub_merk(submerkkendaraanField.getText().trim());
		detailKendaraan.setKendaraan(getKendaraan());

		return detailKendaraan;
	}

	protected enum Choice {
		JENIS_KENDARAAN, JENIS_SPAREPART
	}
}